﻿Imports LRS.BLL
Imports LRS.DTO
Imports System.Windows.Forms

Public Class SelectProduct
    Private oldRowIndex As Integer = -1
    Private Sub SelectProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gridProduct.AutoGenerateColumns = False
        gridProduct.DataSource = bProduct.GetProducts()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        gridProduct.DataSource = bProduct.GetProductsByKeyword(txtSearch.Text.Trim())
    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        If (gridProduct.SelectedCells.Count = 0) Then
            MessageBox.Show("Vui lòng chọn một sản phẩm")
            Return
        End If
        
        Dim product As eProduct = New eProduct()
        product = bProduct.GetProductByID(Integer.Parse(gridProduct.Rows(gridProduct.SelectedCells(0).RowIndex).Cells("ProductID").Value.ToString()))
        If (product.Quantity > 0) Then
            Dim orderForm As CreateOrder = Me.Owner
            If (orderForm.AddProduct(product)) Then
                lblMessage.Text = "Đã thêm sản phẩm số hiệu " + product.ProductNumber + " vào đơn hàng"
            Else
                lblMessage.Text = ""
            End If
        Else
            MessageBox.Show("Hết hàng trong kho")
            Return
        End If
        
    End Sub

    Private Sub gridProduct_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridProduct.CellEnter
        If (e.RowIndex <> oldRowIndex) Then
            Dim product As eProduct = bProduct.GetProductByID(Int32.Parse(gridProduct.Rows(e.RowIndex).Cells("ProductID").Value.ToString()))
            ProductInfo1.DisplayInfo(product)
            oldRowIndex = e.RowIndex
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class